#include <stdio.h>

#include "sockclient.h"

SocketClient::SocketClient(int addressFamily, int type, int protocol) 
: Socket(addressFamily, type, protocol) {

}

bool SocketClient::openConnection(const char * ip, int port) {
	struct sockaddr_in sin;
	memset(&sin, 0, sizeof(sin));
	sin.sin_family = this->addressFamily;
	sin.sin_port = htons(port);

	struct hostent * hp;
	if ((hp = gethostbyname(ip)) == 0) {
		perror("gethostbyname");
		return false;
	}
	sin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;

	if (connect(this->sockData, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
		perror("connect");
		return false;
	}

	this->setIpAddress(ip);

	return true;
}
